package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ServiceType;
import ywpt.web.common.PublicFunction;
@Service("serviceTypeService")
@RemoteProxy(name="serviceTypeService")
public class ServiceTypeServiceImpl extends HibernateEntityDao<ServiceType> implements ServiceTypeService{

	@Override
	public ServiceType getServiceTypeInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public String createTypeCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ServiceType.class, "service");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ServiceType> serviceInfos = this.getServiceInfos(query);
		if ( serviceInfos.iterator().hasNext() )	
		{
			code = (serviceInfos.iterator().next().getTypeCode()).substring(4);
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return ("FWLX"+typeCode);
	}

	@Override
	public int getServiceTypeInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ServiceType> getServiceTypeInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ServiceType> getServiceTypeInfos() {
		return this.getAll();
	}

	@Override
	public void removeServiceTypeInfo(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveServiceTypeInfo(ServiceType serviceTypeInfo) {
		this.save(serviceTypeInfo);
	}

	@Override
	public void updateServiceTypeInfo(ServiceType serviceTypeInfo) {
		this.update(serviceTypeInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ServiceType> getServiceInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<ServiceType> getServiceTypesByTypeName(String typeName,
			int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ServiceType.class, "servicetype");
		//query.add(Restrictions.eq("organType", "01"));
		if ( typeName != null && !typeName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("typeName", "%" + typeName + "%"),  
							          Restrictions.like("typeCode", "%" + typeName + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("typeCode"));
		List<ServiceType> serviceTypeList = new ArrayList<ServiceType>();
		List<ServiceType> serviceTypeLists = new ArrayList<ServiceType>();
		ServiceType serviceType =new ServiceType();
		
		serviceTypeList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ServiceType> itr = serviceTypeList.iterator(); itr.hasNext(); i++ ) 
		{
			serviceType = itr.next();
			String ss = PublicFunction.format(serviceType.getTypeCode() + "", 14, "  ", 1)
			          //+ PublicFunction.format(organ.getOrganName() + "", 14, "  ", 1);
			          + serviceType.getTypeName(); 
			
			serviceType.setTypeName(ss);
			serviceTypeLists.add(serviceType);
		}
		return serviceTypeLists;
	}

	@Override
	@RemoteMethod
	public int getServiceTypesCountByName(String typeName) {
		DetachedCriteria query = DetachedCriteria.forClass(ServiceType.class, "servicetype");
		//query.add(Restrictions.eq("organType", "01"));
		if ( typeName != null && !typeName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("typeName", "%" + typeName + "%" ),  
							          Restrictions.like("typeCode", "%" + typeName + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		query.setProjection(null);
		return 	this.getCountByCriteria(query);
	}

}
